package com.hkd.base_service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkd.base_service.api.AreaService;
import com.hkd.base_service.api.dto.AreaUploadDTO;
import com.hkd.base_service.api.entity.Area;
import com.hkd.base_service.api.vo.AreaGetVo;
import com.hkd.base_service.mapper.AreaMapper;
import com.hkd.common.api.ApiResponse;
import com.hkd.common.api.enums.AreaType;
import com.hkd.common.api.constants.CacheConstants;
import com.hkd.utils.CheckUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HKD
 * @date 2022/9/11 1:41
 */
@RestController
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public ApiResponse uploadArea(String curUserId, List<AreaUploadDTO> uploadDTOS) {
        List<Area> areas = new ArrayList<>();
        for (AreaUploadDTO uploadDTO : uploadDTOS) {
            Area area = BeanUtil.copyProperties(uploadDTO, Area.class);
            area.setCreateId(curUserId);
            area.setUpdateTime(LocalDateTime.of(2021, Month.APRIL, 6, 0, 0));
            areas.add(area);
        }
        boolean b = this.saveBatch(areas);
        return b ? ApiResponse.ok() : ApiResponse.error("导入失败");
    }

    @Cacheable(value = CacheConstants.AREA,key = "'a'+#areaCode")
    @Override
    public ApiResponse<List<AreaGetVo>> getAll(String areaCode) {
        List<AreaGetVo> areaGetVos=null;
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Area::getCode, Area::getName);
        AreaType areaType = null;
        if (StringUtils.isEmpty(areaCode)) {
            areaType = AreaType.COUNTRY;
        } else {
            areaType = CheckUtil.areaType(areaCode);
        }
        AreaType childType = AreaType.childType(areaType);
        wrapper.orderByAsc(Area::getCode);
        if (AreaType.COUNTRY==areaType) {
            wrapper.eq(AreaType.COUNTRY == areaType, Area::getAreaType, childType);
            List<Area> areas = this.list(wrapper);
             areaGetVos = BeanUtil.copyToList(areas, AreaGetVo.class);
        }
        if (AreaType.PROVINCE == areaType) {
            areaGetVos = baseMapper.selectCity(areaCode)
                    .stream().filter(item -> !item.getCode().equals(areaCode))
                    .collect(Collectors.toList());
        }
        if (AreaType.CITY == areaType) {
            areaGetVos = baseMapper.selectArea(areaCode)
                    .stream().filter(item -> !item.getCode().equals(areaCode))
                    .collect(Collectors.toList());
        }
        return ApiResponse.ok(areaGetVos);
    }
}
